package com.yupi.yupicturebackend.manager.auth.annotation;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.strategy.SaAnnotationStrategy;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;

@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器, 打开注解式鉴权功能
        System.out.println("=== 注册 Sa-Token 拦截器 ===");
        registry.addInterceptor(new SaInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/api/picture/test-no-auth"); // 排除无权限测试接口
        System.out.println("Sa-Token 拦截器注册完成");
    }

    /**
     * 重写 Sa-Token 注解处理器, 增加直接合并功能
     */
    @PostConstruct
    public void rewriteSaStrategy() {
        System.out.println("=== 初始化 Sa-Token 注解策略 ===");
        SaAnnotationStrategy.instance.getAnnotation = AnnotatedElementUtils::getMergedAnnotation;
        System.out.println("Sa-Token 注解合并功能已启用");
    }
}
